package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.*;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //获取分页查询参数
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //将分页参数交给分页助手处理
        PageHelper.startPage(currentPage, pageSize);
        //将查询信息传递给持久层处理并返回结果
        Page<Menu> page = menuDao.findByCondition(queryString);
        //获取总条数
        long total = page.getTotal();
        //获取对象集合
        List<Menu> list = page.getResult();

        //封装成对象并返回给控制层
        PageResult pageResult = new PageResult(total, list);
        return pageResult;
    }

    @Override
    public List<Menu> findChildrenByPid(Integer id) {
        List<Menu> childrens = menuDao.findChildrenByPid(id);
        return childrens;
    }
    //新增菜单
    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }
    //查询所有一级菜单
    @Override
    public List<Menu> findFatherMenus() {
        return menuDao.findFatherMenus();
    }
    //根据id查找菜单
    @Override
    public Menu findById(Integer menuId) {
        return menuDao.findById(menuId);
    }
    //编辑菜单
    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }
    //删除菜单
    @Override
    public void deleteById(Integer id) {
        //查询菜单是否关角色
        Long count = menuDao.findRoleByMenuId(id);
        //判断
        if (count > 0) {
            //如果存在,则不能删除
            throw new RuntimeException("您删除的菜单已绑定"+count+"个角色,无法删除");
        }
        //如果不存在，则删除
        menuDao.deleteById(id);
    }
    //根据用户名查询所有关联的菜单
    public List<Menu> findMenuByUsername(String username) {
        //查询用户信息
        User user = userDao.findUserByUserName(username);
        //通过用户id查询角色信息
        Set<Role> roleList = roleDao.findRoleByUserId(user.getId());
        List<Menu> menuList1 =new ArrayList<>();
        if (roleList != null && roleList.size() > 0) {
            for (Role role : roleList) {
                //通过角色id查询菜单信息
                List<Menu> list= menuDao.findMenuByRoleId(role.getId());
                if (list!=null&&list.size()>0){
                    for (Menu menu : list) {
                        menuList1.add(menu);
                    }
                }
            }
        }
        //查询所有子菜单
        List<Menu> menuList = findChildrenMenus(menuList1);
        return menuList;
    }
//    public List<Menu> findMenuByUsername(String username) {
//        //查询用户信息
//        User user = userDao.findUserByUserName(username);
//        //通过用户id查询角色信息
//        Set<Role> roleList = roleDao.findRoleByUserId(user.getId());
//        List<Menu> menuList =new ArrayList<>();
//        if (roleList != null && roleList.size() > 0) {
//            for (Role role : roleList) {
//                //通过角色id查询菜单信息
//                List<Menu> listpar= menuDao.findMenuByRoleId(role.getId());
//                List<Menu> listchi= menuDao.findchildren(role.getId());
//                    for (Menu menupar : listpar) {
//                        for (Menu menuchildren : listchi) {
//                            if (menupar!=null&&menuchildren!=null&&menupar.getId()==menuchildren.getParentMenuId()){
//                                menupar.setChildren(listchi);
//                                menuList.add(menupar);
//                            }
//                        }
//                    }
//
//            }
//        }
//        return menuList;
//    }


    //查询所有子菜单
    public List<Menu> findChildrenMenus(List<Menu> menuList) {
        //遍历菜单列表
        menuList.forEach(menu -> {
            List<Menu> childrenMenus = menuDao.findChildrenMenus(menu.getId());
            if (!childrenMenus.isEmpty()) {
                //用户没有此子菜单权限，则从集合中移除
                Iterator<Menu> childMenuIterator = childrenMenus.iterator();
                while (childMenuIterator.hasNext()) {
                    Menu cMenu = childMenuIterator.next();
                    menuList.forEach(topMenu->{
                        if (topMenu.getId().equals(cMenu.getId())) {
                            childMenuIterator.remove();
                        }
                    });
                }
                //子菜单不为空，将子菜单列表赋值给菜单的子菜单列表属性
                menu.setChildren(childrenMenus);
                //迭代子菜单列表，查询子菜单的下级菜单，子菜单为空则弹栈
                findChildrenMenus(childrenMenus);
            }
        });
        return menuList;
    }
    @Override
    public List<Menu> findAll() {

        return menuDao.findAll();
    }
}
